找传奇、传世资源到传世资源站!

C#做的一个推箱子的小游戏

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

本推箱子游戏使用数组实现,用不同的数字标识不同的物体,从而简化了人物移动的判断过程from clipboard
from clipboard from clipboard
from clipboard from clipboard from clipboard
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Media;namespace 推箱子2._0{ public partial class Form1 : Form { public const int len=10;//数组长度 int [,] num = new int[len,len];//初始化数组,数组元素全为0 public const int height = 50, width = 50;//墙的长宽 int row,col;//标记人物位置 int direction = 2;//方向1,2,3,4代表上下左右 //SoundPlayer player = new SoundPlayer(); int leval = 1;//关卡等级标记 int map = 1;//地图 public Form1() { InitializeComponent(); initNum(); this.KeyPreview = true; this.BackgroundImage = new Bitmap(@"map1\floor1.png");//初始化窗口背景 } /* 初始化界面 */ private void initGameView() { Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);//创建一个位图,作为PictureBox的Image label2.Text = "第 " leval " 关"; Bitmap bitmp;//用于承载物体的图片 Graphics g = Graphics.FromImage(bmp); if (map == 2)//地图2 { for (int i = 0; i < len; i ) { for (int j = 0; j < len; j ) { bitmp = new Bitmap(@"map2\block.gif"); if (num[i, j] == 0) bitmp = new Bitmap(@"map2\block.gif"); if (num[i, j] == 1) bitmp = new Bitmap(@"map2\wall.png"); if (num[i, j] == 2) bitmp = new Bitmap(@"map2\box.png"); if (num[i, j] == 3) bitmp = new Bitmap(@"map2\ball.png"); if (num[i, j] == 4) { switch (direction) { case 1: bitmp = new Bitmap(@"map2\person_up.png"); break; case 2: // bitmp = new Bitmap(@"Images\pic04down.png"); break; bitmp = new Bitmap(@"map2\person_down.png"); break; case 3: bitmp = new Bitmap(@"map2\person_left.png"); break; case 4: bitmp = new Bitmap(@"map2\person_right.png"); break; } row = i; col = j; } if (num[i, j] == 5) bitmp = new Bitmap(@"map2\person5.jpg"); if (num[i, j] == 6) bitmp = new Bitmap(@"map2\box.png"); g.DrawImage(bitmp, i * width, j * height, width, height); } } pictureBox1.Image = bmp; } if (map == 1)//地图1 { for (int i = 0; i < len; i ) { for (int j = 0; j < len; j ) { bitmp = new Bitmap(@"map1\floor1.png"); if (num[i, j] == 0) bitmp = new Bitmap(@"map1\floor1.png"); if (num[i, j] == 1) bitmp = new Bitmap(@"map1\wall2.png"); if (num[i, j] == 2) bitmp = new Bitmap(@"map1\pic02.png"); if (num[i, j] == 3) bitmp = new Bitmap(@"map1\pic03.jpg"); if (num[i, j] == 4) { switch (direction) { case 1: bitmp = new Bitmap(@"map1\pic04up.png"); break; case 2: // bitmp = new Bitmap(@"Images\pic04down.png"); break; bitmp = new Bitmap(@"map1\pic04down.png"); break; case 3: bitmp = new Bitmap(@"map1\pic04left.png"); break; case 4: bitmp = new Bitmap(@"map1\pic04right.png"); break; } row = i; col = j; } if (num[i, j] == 5) bitmp = new Bitmap(@"map1\pic05.png"); if (num[i, j] == 6) bitmp = new Bitmap(@"map1\pic06.png"); g.DrawImage(bitmp, i * width, j * height, width, height);//将物体画到大背景 } } pictureBox1.Image = bmp;//设置pictureBox的Image属性 } } /* 初始化数组 */ public void initNum() { for (int i = 0; i < len; i ) { for (int j = 0; j < len; j ) { num[i, j] = 0; if (i == 0 || j == 0 || i == 9 || j == 9) num[i, j] = 1; } } switch (leval)//基于等级显示不同的关卡 { case 1: { num[3, 5] = 4; num[5, 6] = num[5, 7] =num[5,8] = num[5,4] = num[3,3] = num[4,3] =num[7,5] =num[8,5] =num[6,2] = num[7,7]= 1; num[6, 6] = num[4, 7] = 2; num[7, 2] = num[3, 4] = 3; break; } case 2: { num[3, 3] = num[5, 5] = num[6, 5] = num[6, 6] = num[8,3] =num[7,3]= 1 ; num[3, 5] = 4; num[3, 4] = num[5, 4] = 2; num[6,7] = num[4, 4] = 3; break; } case 3: { num[2, 2] = num[4, 3] = num[5, 6] = num[5,7] = num[4, 6] =num[3,4] = num[4,4] = num[4,1] = num[5,3] = num[6,2] = num[7,5]= 1; num[4,2] = 4; num[3, 2] = num[2, 4] = 2; num[3, 3] = num[7, 4] = 3; break; } case 4: { num[1, 2] = num[3, 3] = num[4, 6] = num[5, 6] = num[5, 7] = num[5, 4] = num[2, 5] = num[7, 2] = num[6, 2] = num[6, 4] = num[7, 5] = num[7, 6] = num[6, 1] = num[7, 1] = num[8, 1] = num[6, 2] = num[7, 2] = num[8, 2] = 1; num[3, 5] = 4; num[3, 2] = num[2, 4] = 2; num[3, 7] = num[6, 5] = 3; break; } case 5: { num[1, 3] = num[4, 3] = num[4, 7] = num[2, 7] = num[5, 6] = num[2, 5] = num[2, 6] = num[6, 1] = num[7, 1] = num[8, 1] = num[6, 2] = num[7, 2] = num[8, 2] =num[3,3] = num[6,8]=num[7,8]=num[8,8] =num[7,7]=num[8,7]=num[6,4]=num[7,4]= num[1,4]= 1; num[2,1] = 4; num[3, 2] = num[2,2] = 2; num[1, 8] = num[3, 7] = 3; break; } case 6: { num[1, 8] = num[2, 8] = num[2, 9] = num[1, 1] = num[3, 6] = num[2, 6] = num[2,5]= num[4, 6] = num[5, 6] = num[3, 3] = num[5,2] =num[1,3] = num[4,5] =num[7,4] = num[7,5]=num[7,6]= 1; num[3, 5] = 4; num[3, 2] = num[4,3] = 2; num[1, 7] = num[2, 7] = 3; break; } case 7: { num[1, 8] = num[2, 8] = num[2, 9] = num[1, 1] = num[2, 6] = num[2, 5] = num[4, 6] = num[5, 6] = num[3, 3] = num[5, 2] = num[1, 3] = num[4, 5] = num[7, 4] = num[7, 5] = num[7, 6] = 1; num[5,5] = 4; num[4, 2] = num[6,2 ] = 2; num[4, 7] = num[3,6] = 3; break; } case 8: { num[1, 8] = num[2, 8] = num[2, 9] = num[1, 1] = num[2, 6] = num[2, 5] = num[5, 6] = num[3, 3] = num[5, 2] = num[1, 3] = num[4, 5] = num[7, 4] = num[7, 5] =num[1,4]=num[1,5]=num[1,7]=num[2,7]= 1; num[1,1] = 4; num[2, 2] = num[3, 2] = 2; num[7, 6] = num[4, 6] = 3; break; } case 9: { num[1, 8] = num[2, 8] = num[2, 9] = num[2, 6] = num[2, 5] = num[5, 6] = num[3, 3] = num[5, 2] = num[1, 3] = num[4, 5] = num[7, 4] = num[7, 5] = num[1, 4] = num[1, 5] = num[1, 7] = num[2, 7] =num[8,8]=num[8,3]=num[8,2]=num[8,1]=num[7,3]= 1; num[5, 5] = 4; num[2, 2] = num[3, 2] = 2; num[8, 4] = num[8, 5] = 3; break; } case 10: { num[1, 8] = num[2, 8] = num[2, 9] = num[2, 6] = num[2, 5] = num[5, 6] = num[3, 3] = num[5, 2] = num[1, 3] = num[4, 5] = num[7, 4] = num[7, 5] = num[1, 4] = num[1, 5] = num[1, 7] = num[2, 7] = num[8, 8] = num[8, 3] = num[8, 2] = num[8, 1] = num[7, 3] =num[5,7]=num[7,6]=num[8,5]=num[8,7]=num[7,7] =num[5,3]= 1; num[2, 6] = 4; num[2, 2] = num[3, 2] = 2; num[6, 5] = num[4, 4] = 3; break; } case 11: { num[2, 3] = num[1, 5] = num[3, 4] =num[4,4]= num[4, 6] = num[5, 6] = num[7, 6] = num[5, 2] = num[6, 2] = num[6, 3] = num[8, 6] =num[4,2] =num[6,5]= 1; num[3, 7] = num[7, 6] = 2; num[1, 1] = num[8, 2] = 3; num[1, 2] = 4; break; } case 12: { num[3,1] = num[3,2] = num[3, 4] = num[4, 2] = num[6,2] = num[5, 3] = num[8,3] = num[1,4] = num[1,5] = num[2,7] = num[4, 6] = num[6,6] = num[6, 5] =num[7,7]=num[8,8] = num[7,8] =num[6,7]=num[6,8]=num[5,8]= 1; num[3, 3] = num[2,2] = 2; num[4, 1] = num[2,8] = 3; num[2,3] = 4; break; } case 13: { num[1, 8] = num[2, 8] = num[2, 9] = num[1, 1] = num[3, 6] = num[2, 6] = num[2, 5] = num[4, 6] = num[5, 6] = num[3, 3] = num[5, 2] = num[1, 3] = num[4, 5] = num[7, 4] = num[7, 5] = num[7, 6] =num[7,3]=num[7,2]=num[7,8]=num[5,1] = num[5,5]= 1; num[4,2] = 4; num[3, 2] = num[4, 3] = num[5,4]= 2; num[1, 7] = num[3, 7] = num[8,8]=3; break; } } } /* 判断是否通关 */ public void isWinner() { int count = 0; //遍历数组,统计为6的个数,即箱子放在洞上 for (int i = 0; i < len; i ) { for (int j = 0; j < len; j ) { if (num[i, j] == 6) count ; } } if (leval <= 12)//12级以前只有两个箱子 { if (count == 2) { DialogResult result = MessageBox.Show("过关了!是否进行下一关?","过关啦",MessageBoxButtons.OKCancel,MessageBoxIcon.Information); if (result == DialogResult.OK) { num = new int[len, len]; leval ; direction = 2; initNum(); initGameView(); } } } else if (leval < 20)//13级到20级有三个箱子 { if (count == 3) { DialogResult result = MessageBox.Show("过关了!是否进行下一关?", "过关啦", MessageBoxButtons.OKCancel,MessageBoxIcon.Information); if (result == DialogResult.OK) { num = new int[len, len]; leval ; direction = 2; initNum(); initGameView(); } } } } //x1,y1为当前人物的下一个,x2,y2为当前人物的下两个 public void PersonMove(int x1, int y1, int x2, int y2) { int next1=num[x1,y1]; if(next1==1) return; int next2=num[x2,y2]; if (next1 == 0) { num[x1, y1] = 4; AfterMove(row, col); } else if (next1 == 3) { num[x1, y1] = 5; AfterMove(row, col); } else if (next1 == 2 && next2 == 0) { num[x2, y2] = 2; num[x1, y1] = 4; AfterMove(row, col); } else if (next1 == 2 && next2 == 3) { num[x2, y2] = 6; num[x1, y1] = 4; AfterMove(row, col); } else if (next1 == 6 && next2 == 0) { num[x2, y2] = 2; num[x1, y1] = 5; AfterMove(row, col); } else if (next1 == 6 && next2 == 3) { num[x2, y2] = 6; num[x1, y1] = 5; AfterMove(row, col); } row = x1; col = y1; } //人物移动后,当前位置的图片 public void AfterMove(int row, int col) { if (num[row, col] == 4)//当前位置是4,变为0,即人没有在箱子上 num[row, col] = 0; if (num[row, col] == 5)//当前位置是5,变为3,即人在箱子上 num[row, col] = 3; } private void Form1_KeyDown(object sender, KeyEventArgs e) { int x1=0, y1=0, x2=0, y2=0; switch (e.KeyCode) { case Keys.Left: direction = 3; x1 = row - 1; y1 = col; x2 = row - 2; y2 = col; break; case Keys.Right: direction = 4; x1 = row 1; y1 = col; x2 = row 2; y2 = col; break; case Keys.Up: direction = 1; x1 = row; y1 = col - 1; x2 = row; y2 = col - 2; break; case Keys.Down: direction = 2; x1 = row; y1 = col 1; x2 = row; y2 = col 2; break; } PersonMove(x1, y1, x2, y2); initGameView(); isWinner(); } private void label1_Click(object sender, EventArgs e) { direction = 2; num = new int[10, 10]; initNum(); initGameView(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { int dex = listBox1.SelectedIndex; leval = dex 1; direction = 2; num = new int[len, len]; initNum(); initGameView(); pictureBox1.Focus(); tabControl1.Visible = false; } private void label3_Click(object sender, EventArgs e) { tabControl1.Visible = true; } private void label4_Click(object sender, EventArgs e) { this.Close(); } private void label5_Click(object sender, EventArgs e) { leval ; direction = 2; num = new int[len, len]; initNum(); initGameView(); tabControl1.Visible = false; tabControl1.Enabled = false; } private void label6_Click(object sender, EventArgs e) { if (leval != 1) { leval--; direction = 2; num = new int[10, 10]; initNum(); initGameView(); tabControl1.Visible = false; tabControl1.Enabled = false; } } private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked) { map = 1; this.BackgroundImage = new Bitmap(@"map1\floor1.png"); initGameView(); pictureBox1.Focus(); } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked) { map = 2; this.BackgroundImage = new Bitmap(@"map2\block.gif"); initGameView(); pictureBox1.Focus(); } } private void button1_Click(object sender, EventArgs e) { pictureBox1.Focus(); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复